package com.example.tcp.client.serde.impl;

import com.example.tcp.client.RetryTcpClient;
import io.netty.buffer.Unpooled;
import org.jetbrains.annotations.NotNull;
import org.mapdb.DataInput2;
import org.mapdb.DataOutput2;
import org.mapdb.Serializer;

import java.io.IOException;
import java.io.Serializable;

public class MapDBMessageSerde implements Serializer<RetryTcpClient.Message>, Serializable {
    @Override
    public void serialize(@NotNull DataOutput2 dataOutput2, @NotNull RetryTcpClient.Message message) throws IOException {
        dataOutput2.write(message.serialize());
    }

    @Override
    public RetryTcpClient.Message deserialize(@NotNull DataInput2 dataInput2, int available) throws IOException {
        byte[] value = new byte[available];
        dataInput2.readFully(value);
        return new RetryTcpClient.ByteBufMessage(Unpooled.copiedBuffer(value));
    }
}
